﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace PlantsVsZombies
{
    public class Sprite
    {
        #region "Variable"

        private Texture2D[] _textures;
        private Vector2 _position;
        private Vector2 _size;

        private int _nTexture;
        private int _iTextture;

        // bien luu texture thu _state duoc chon de ve
        // _state = -1 => ve tu dong
        // _state >-1 => ve texture thu _state
        private int _state = -1;

        private bool _visible = true;

        #endregion

        #region "Properties"

        public Texture2D[] Textures
        {
            get { return _textures; }
            set
            {
                _textures = value;
                _nTexture = _textures.Length;
                _iTextture = 1;
            }
        }

        public int NTexture
        {
            get { return _nTexture; }
            set { _nTexture = value; }
        }


        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public int State
        {
            get { return _state; }
            set { _state = value; }
        }

        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }

        #endregion

        public Sprite(Texture2D[] textures, Vector2 pos, Vector2 size)
        {
            InitSprite(textures, pos, size);
        }

        private void InitSprite(Texture2D[] textures, Vector2 pos, Vector2 size)
        {
            _textures = textures;
            _nTexture = textures.Length;
            _position = pos;
            _size = size.X != 0 ? size : new Vector2(_textures[0].Width, _textures[0].Height);
        }

        public Sprite(ContentManager contentManager, string[] textures, int nTexture, Vector2 pos, Vector2 size)
        {

            Texture2D[] tempTextures = new Texture2D[nTexture];

            for (int i = 0; i < nTexture; i++)
                tempTextures[i] = contentManager.Load<Texture2D>(textures[i]);

            InitSprite(tempTextures,pos,size);

           

        }

        public Sprite(ContentManager contentManager, string texture, Vector2 pos, Vector2 size)
        {
            string[] tempString = new string[1];
            tempString[0] = texture;

            Texture2D[] tempTextures = new Texture2D[1];
            tempTextures[0] = contentManager.Load<Texture2D>(tempString[0]);
            InitSprite(tempTextures, pos, size);
            
        }

        public float NormalDelay = 16.0f;
        private int _delay = 0;
        public void Update(GameTime gameTime)
        {
            var delta = (int) (gameTime.TotalGameTime.Milliseconds/NormalDelay);
            if (_delay <= 8)
                _delay++;
            else
            {
                _iTextture = delta % _nTexture;
                _delay = 0;
            }
            
        }

        public void Draw(SpriteBatch spritebatch)
        {
           if(Visible)
                 spritebatch.Draw(State == -1 ? _textures[_iTextture] : _textures[State],
                             new Rectangle((int) _position.X, (int) _position.Y, (int) Size.X, (int) Size.Y),
                             Color.White);

            
        }

        public void Draw(SpriteBatch spritebatch, Color color)
        {
            if (Visible)
                spritebatch.Draw(State == -1 ? _textures[_iTextture] : _textures[State],
                            new Rectangle((int)_position.X, (int)_position.Y, (int)Size.X, (int)Size.Y),
                            color);


        }

        

        public bool IsClicked(Vector2 point)
        {
            if ((point.X >= Position.X) &&
                (point.X <= Position.X + _size.X) &&
                (point.Y >= Position.Y) &&
                (point.Y <= Position.Y + _size.Y))
                return true;
            return false;
        }

        public bool IsCollision(Sprite sprite)
        {
            return false;
        }

       
     
    }
}
